package com.plum.admin.modules.social.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.social.entity.SysSocial;
import com.plum.admin.modules.social.entity.SysSocialRelate;
import com.plum.admin.modules.social.service.ISysSocialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.List;

/**
 * <p>
 * 社交登录表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/sys/relate")
@Api(tags = "第三方账号相关API")
public class SysSocialController extends BaseController {
    @Autowired
    private ISysSocialService sysSocialService;

    @GetMapping("/relateList/{username}")
    @ApiOperation("获取第三方账号绑定详情")
    public R<Object> relateList(@PathVariable String username){
        List<SysSocial> list = sysSocialService.list(Wrappers.<SysSocial>lambdaQuery().eq(SysSocial::getUserName,username));
        SysSocialRelate relate=new SysSocialRelate();
        list.forEach(data->{
            if(data.getPlatform().equals(Constant.SOCIAL_TYPE_ALIPAY)){
                relate.setAlipayRelate(true).setAlipayUserName(data.getNickName()).setAlipayId(data.getId());
            }
            if(data.getPlatform().equals(Constant.SOCIAL_TYPE_WECHAT)){
                relate.setWechatRelate(true).setWechatUserName(data.getNickName()).setWechatId(data.getId());
            }
            if(data.getPlatform().equals(Constant.SOCIAL_TYPE_WEIBO)){
                relate.setWeiboRelate(true).setWeiboUserName(data.getNickName()).setWeiboId(data.getId());
            }
            if(data.getPlatform().equals(Constant.SOCIAL_TYPE_GITEE)){
                relate.setGiteeRelate(true).setGiteeUserName(data.getNickName()).setGiteeId(data.getId());
            }
        });
        return R.ok(relate);
    }
    @PostMapping("/unRelate/{id}")
    @ApiOperation("解除绑定")
    public R<Object> unRelate(@PathVariable String id){
        return R.ok(sysSocialService.removeById(id));
    }
}
